package zhr.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import zhr.store.Dto.SetmealDto;
import zhr.store.domain.Setmeal;
import zhr.store.domain.SetmealDish;
import zhr.store.service.SetmealDishService;
import zhr.store.service.SetmealService;
import zhr.store.mapper.SetmealMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 詹华榕
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2023-04-16 14:36:58
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增套餐表同时新增套餐菜品关系表
     */
    @Override
    public void insertSetMealAndDish(SetmealDto setmealDto) {
        //新增套餐表
        save(setmealDto);
        //新增套餐菜品关系表
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealDto selectById(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        //查询套餐基本信息
        Setmeal setmeal = super.getById(id);
        //查询套餐菜品关系信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);
        //拷贝
        BeanUtils.copyProperties(setmeal, setmealDto);
        return setmealDto;
    }

    /**
     * 修改套餐
     */
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //修改dish表
        updateById(setmealDto);
        //修改SetmealDish表
        //先清除信息,再新增
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        //新增
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐
     */
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //删除套餐表
        removeByIds(ids);
        //删除关系表
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }
}




